﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

using H2.NET.Controls;
using H2.NET.Data.Common;
using H2.NET.Business;

namespace H2.NET.Web.Pages
{
    public partial class TabHierarchy : TaskControl
    {
        #region 属性 Public
        public MDCTabStatus Status
        {
            get
            {
                if (StoreAPI["Status~" + Key] == null)
                {
                    StoreAPI["Status~" + Key] = new MDCTabStatus();
                }
                return (MDCTabStatus)StoreAPI["Status~" + Key];
            }
            set
            {
                Key = value.KeyWord;
                StoreAPI["Status~" + Key] = value;
            }
        }

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

        public int SheetIndex
        {
            get
            {
                return ViewState["SheetIndex"] == null ? 0 : (int)ViewState["SheetIndex"];

            }
            set { ViewState["SheetIndex"] = value; }
        }
        #endregion

        #region 事件 页面加载
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!Status.IsUsed)
            {
                return;
            }

        }
        protected override void OnLoad(EventArgs e)
        {

            InitControls();
            base.OnLoad(e);

        }

        public void InitControls()
        {


            #region Hierarchy
                if (Status.Category == "HIERARCHY")
                {
                    bool flg2 = Status.Marketing == "C" && TaskAPI.IsAllowSubmit;
                    if (TaskAPI.Process == "TW Group-Hierarchy-Cluster")
                    {
                        gv_Product_Hierarchy.DynamicColumn.Add(new BoundColumn2("00", "No", BoundColumnType2.TextBox, 60, Align.Left, flg2, "No", InputType.None, null));
                        gv_Product_Hierarchy.DynamicColumn.Add(new BoundColumn2("02", "Ph3 descrip EN", BoundColumnType2.TextBox, 150, Align.Left, flg2, "Product_Line", InputType.None, null));
                        gv_Product_Hierarchy.DynamicColumn.Add(new BoundColumn2("03", "PH3 descrip ZF", BoundColumnType2.TextBox, 150, Align.Left, flg2, "Sub_Line", InputType.None, null));
                        gv_Product_Hierarchy.DynamicColumn.Add(new BoundColumn2("04", "PH4 descrip EN", BoundColumnType2.TextBox, 250, Align.Left, flg2, "Sub_Line_Des", InputType.None, null));
                        gv_Product_Hierarchy.DynamicColumn.Add(new BoundColumn2("05", "PH4 descrip ZF", BoundColumnType2.TextBox, 250, Align.Left, flg2, "Description_in_ZH", InputType.None, null));
                        SetEnable(flg2, Product_Hierarchy_Add);
                        SetEnable(flg2, Product_Hierarchy_Delete);
                    }
                    else
                    {
                        gv_Product_Hierarchy.DynamicColumn.Add(new BoundColumn2("00", "No", BoundColumnType2.TextBox, 60, Align.Left, flg2, "No", InputType.None, null));
                        gv_Product_Hierarchy.DynamicColumn.Add(new BoundColumn2("02", "Product Line", BoundColumnType2.TextBox, 150, Align.Left, flg2, "Product_Line", InputType.None, null));
                        gv_Product_Hierarchy.DynamicColumn.Add(new BoundColumn2("03", "Sub Line", BoundColumnType2.TextBox, 150, Align.Left, flg2, "Sub_Line", InputType.None, null));
                        gv_Product_Hierarchy.DynamicColumn.Add(new BoundColumn2("04", "Sub Line Des", BoundColumnType2.TextBox, 250, Align.Left, flg2, "Sub_Line_Des", InputType.None, null));
                        gv_Product_Hierarchy.DynamicColumn.Add(new BoundColumn2("05", "Description in ZH", BoundColumnType2.TextBox, 250, Align.Left, flg2, "Description_in_ZH", InputType.None, null));
                        SetEnable(flg2, Product_Hierarchy_Add);
                        SetEnable(flg2, Product_Hierarchy_Delete);
                    }

                    //gv_Product_Hierarchy.DynamicColumn.Add(new BoundColumn2("00", "No", BoundColumnType2.TextBox, 60, Align.Left, flg2, "No", InputType.None, null));
                    //gv_Product_Hierarchy.DynamicColumn.Add(new BoundColumn2("02", "Product Line", BoundColumnType2.TextBox, 150, Align.Left, flg2, "Product_Line", InputType.None, null));
                    //gv_Product_Hierarchy.DynamicColumn.Add(new BoundColumn2("03", "Sub Line", BoundColumnType2.TextBox, 150, Align.Left, flg2, "Sub_Line", InputType.None, null));
                    //gv_Product_Hierarchy.DynamicColumn.Add(new BoundColumn2("04", "Sub Line Des", BoundColumnType2.TextBox, 250, Align.Left, flg2, "Sub_Line_Des", InputType.None, null));
                    //gv_Product_Hierarchy.DynamicColumn.Add(new BoundColumn2("05", "Description in ZH", BoundColumnType2.TextBox, 250, Align.Left, flg2, "Description_in_ZH", InputType.None, null));
                    //SetEnable(flg2, Product_Hierarchy_Add);
                    //SetEnable(flg2, Product_Hierarchy_Delete);

                    IList<mdc_Product_Hierarchy> product_hierarchy = (List<mdc_Product_Hierarchy>)gv_Product_Hierarchy.DataSource;
                    if (!Page.IsPostBack)
                    {
                        Hierarchy_P10.Visible = true;
                        gv_Product_Hierarchy.Key = Key;

                        #region 发起步骤
                        if (TaskAPI.IsInitiate)
                        {
                            List<mdc_Product_Hierarchy> product_hierarchy_s = new List<mdc_Product_Hierarchy>();
                            for (int i = 0; i < 4; i++)
                            {
                                product_hierarchy_s.Add(new mdc_Product_Hierarchy());
                                product_hierarchy_s[i].No = i + 1;
                            }
                            gv_Product_Hierarchy.DataSource = product_hierarchy_s;
                        }
                        #endregion

                        #region 审批环节
                        else
                        {
                            IList<mdc_Product_Hierarchy> product_hierarchy_s = (mdc_Product_Hierarchy.process == TaskAPI.Process & mdc_Product_Hierarchy.incident == TaskAPI.Incident).List<mdc_Product_Hierarchy>();
                            if (product_hierarchy_s.Count == 0)
                            {
                                for (int i = 0; i < 4; i++)
                                {
                                    product_hierarchy_s.Add(new mdc_Product_Hierarchy());
                                    product_hierarchy_s[i].No = i + 1;
                                }
                            }
                            gv_Product_Hierarchy.DataSource = product_hierarchy_s;

                        }
                        #endregion
                    }
                }
                #endregion
            }
        
        #endregion

        #region 事件 Add(增加行)
        protected void Add_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "Product_Hierarchy_Add":
                    List<mdc_Product_Hierarchy> product_hierarchy_s = (List<mdc_Product_Hierarchy>)gv_Product_Hierarchy.DataSource;
                    mdc_Product_Hierarchy product_hierarchy = new mdc_Product_Hierarchy();
                    product_hierarchy.No = product_hierarchy_s.Count + 1;
                    product_hierarchy_s.Insert(gv_Product_Hierarchy.ChangedRowIndex == -1 || gv_Product_Hierarchy.ChangedRowIndex >= product_hierarchy_s.Count ? product_hierarchy_s.Count - 1 : gv_Product_Hierarchy.ChangedRowIndex, product_hierarchy);
                    gv_Product_Hierarchy.DataSource = product_hierarchy_s;
                    gv_Product_Hierarchy.DataBind();
                    break;
            }
        }
        #endregion

        #region 事件 Delete(删除行)
        protected void Delete_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "Product_Hierarchy_Delete":
                    List<mdc_Product_Hierarchy> product_hierarchy_s = (List<mdc_Product_Hierarchy>)gv_Product_Hierarchy.DataSource;
                    IList<mdc_Product_Hierarchy> list = (mdc_Product_Hierarchy.process == TaskAPI.Process & mdc_Product_Hierarchy.incident == TaskAPI.Incident).List<mdc_Product_Hierarchy>();
                    if (product_hierarchy_s.Count == 1)
                    {
                        Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留最后一个 Sheet ！',false)");
                        return;
                    }
                    product_hierarchy_s.RemoveAt(gv_Product_Hierarchy.ChangedRowIndex == -1 || gv_Product_Hierarchy.ChangedRowIndex >= product_hierarchy_s.Count ? product_hierarchy_s.Count - 1 : gv_Product_Hierarchy.ChangedRowIndex);
                    bool flag = false;
                    foreach (mdc_Product_Hierarchy product_hierarchy_1 in list)
                    {
                        if (string.IsNullOrEmpty(product_hierarchy_1.Product_Line))
                        {
                            continue;
                        }
                        foreach (mdc_Product_Hierarchy product_hierarchy_2 in product_hierarchy_s)
                        {

                            if (string.IsNullOrEmpty(product_hierarchy_1.Product_Line))
                            {
                                continue;
                            }
                            if (product_hierarchy_2.ID == product_hierarchy_1.ID)
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (!flag)
                        {
                            Storage.Delete(product_hierarchy_1);
                        }
                        flag = false;
                    }
                    gv_Product_Hierarchy.DataSource = product_hierarchy_s;
                    gv_Product_Hierarchy.DataBind();
                    break;
            }
        }
        #endregion

        #region 方法 Save(保存数据库)
        public void Save()
        {
            if (!Status.IsEdited)
            {
                return;
            }

            switch (Status.Category)
            {

                #region Hierarchy
                case "HIERARCHY":
                    foreach (mdc_Product_Hierarchy product_hierarchy in (List<mdc_Product_Hierarchy>)gv_Product_Hierarchy.DataSource)
                    {
                        //if (string.IsNullOrEmpty(product_hierarchy.Product_Line))
                        //{
                        //    continue;
                        //}
                        product_hierarchy.Process = TaskAPI.Process;
                        product_hierarchy.Incident = TaskAPI.Incident;
                        product_hierarchy.UserName = UserAPI.UserName;
                        product_hierarchy.Time = System.DateTime.Now;
                        product_hierarchy.Status = 0;
                        Storage.Save(product_hierarchy);
                    }
                    break;
                #endregion
            }
        }
        #endregion

        #region 方法 DataValidation(数据验证)
        public List<string> DataValidation()
        {
            //如果当前 Tabs 是隐藏的情况，退出
            //if (!Status.IsEdited)
            //{
            //    return new List<string>();
            //}

            List<string> vald_s = new List<string>();
            List<Regular> regs = new List<Regular>();

            //if (Status.Category == "MTO-SEFI")
            //{
            //    regs.Add(new Regular(RegularExp.NotEmpty,  MTO_SEFI_Base_Quantity.Text, "&nbsp;&nbsp;Base Quantity 为必填"));
            //    regs.Add(new Regular(RegularExp.NotEmpty, MTO_SEFI_Plant.Text, "&nbsp;&nbsp;Plant 为必填"));
            //    regs.Add(new Regular(RegularExp.NotEmpty, MTO_SEFI_MTO_Code.Text, "&nbsp;&nbsp;MTO Code 为必填"));

            //    foreach (mdc_Bom_MTO_Sefi_2 mto_sefi in (List<mdc_Bom_MTO_Sefi_2>)gv_MTO_SEFI.DataSource)
            //    {
            //        if (string.IsNullOrEmpty(mto_sefi.Ict) && string.IsNullOrEmpty(mto_sefi.Routing_Text) && string.IsNullOrEmpty(mto_sefi.Component_SAP_Code))
            //        {
            //            continue;
            //        }

            //        if (!string.IsNullOrEmpty(mto_sefi.Ict) && mto_sefi.Ict == "L")
            //        {
            //            mto_sefi.Routing_Text = "";
            //            regs.Add(new Regular(RegularExp.NotEmpty, mto_sefi.Component_SAP_Code, "&nbsp;&nbsp;行" + mto_sefi.No + ": Component SAP Code 为必填"));
            //            regs.Add(new Regular(RegularExp.NotEmpty, mto_sefi.Quantity, "&nbsp;&nbsp;行" + mto_sefi.No + ": Quantity 为必填"));
            //            regs.Add(new Regular(RegularExp.NotEmpty, mto_sefi.Component_Description, "&nbsp;&nbsp;行" + mto_sefi.No + ": Component Description 为必填"));
            //        }

            //        if (!string.IsNullOrEmpty(mto_sefi.Ict) && mto_sefi.Ict == "T")
            //        {
            //            mto_sefi.Component_Description = "";
            //            mto_sefi.Component_SAP_Code = "";
            //            regs.Add(new Regular(RegularExp.NotEmpty, mto_sefi.Routing_Text, "&nbsp;&nbsp;行" + mto_sefi.No + ": Routing Text为必填"));

            //        }
            //    }

            //    if (string.IsNullOrEmpty(((List<mdc_Bom_MTO_Sefi_2>)gv_MTO_SEFI.DataSource)[0].Ict))
            //    {
            //        regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;行" + "BOM: 明细行中至少填写 1 行数据"));
            //    }

            vald_s.AddRange(Validation(regs));

            if (vald_s.Count > 0)
            {
                vald_s.Insert(0, "<font color='#0000FF'><b>" + Status.Name + "</b></font>");
                vald_s.Add("&nbsp;&nbsp;-----------------------------------------");
            }

            return vald_s;
        }
        #endregion
    }
}